/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.formula.file.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.risesoft.risebi.collection.formula.generate.FormulaGenerateException;
import net.risesoft.risebi.collection.formula.generate.Generator;
import net.risesoft.risebi.collection.model.Formula;
import net.risesoft.risebi.collection.model.Template;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Repository(value="calculateGroovyCreator")
public class CalculateGroovyCreator extends AbstractCreator {
	
	private Generator serverFormulaGenerator;
	
	@Autowired
	public void setServerFormulaGenerator(@Qualifier("serverFormulaGenerator")Generator serverFormulaGenerator) {
		this.serverFormulaGenerator = serverFormulaGenerator;
	}

	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.groovy.AbstractGroovyCreator#getClassName(java.lang.String)
	 */
	@Override
	public String getClassName(String templateGuid) {
		String className = "G"+templateGuid+"Calculate";
		return className;
	}

	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.groovy.AbstractGroovyCreator#getFileName(java.lang.String, java.lang.String)
	 */
	@Override
	public String getFileName(String templateFilePath, String templateGuid) {
		String fileName = templateFilePath + templateGuid + "/" +"G"+templateGuid+"Calculate.groovy";
		return fileName;
	}
	
	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.formula.file.impl.AbstractCreator#getPureFileName(java.lang.String)
	 */
	@Override
	public String getPureFileName(String templateGuid) {
		return "G"+templateGuid+"Calculate.groovy";
	}

	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.formula.file.impl.AbstractCreator#newFile(net.risesoft.risebi.collection.model.Template)
	 */
	@Override
	public List<FormulaGenerateException> newFile(Template template) throws Exception {
		List<Formula> formulas = template.getFormulas();
		Map<String,String> formulaMap = new HashMap<String,String>();
		List<FormulaGenerateException> exceptionList = new ArrayList<FormulaGenerateException>();
		for(int i=0;i<formulas.size();i++){
			Formula formula = formulas.get(i);
			if((formula.getRunat()==Formula.RUNAT_SERVER||formula.getRunat()==Formula.RUNAT_BOTH)&&formula.getType()==Formula.TYPE_JISUANGONGSHI){
				try {
					String rString = serverFormulaGenerator.generateResult(formula.getFormula());
					formulaMap.put(formula.getGuid(), rString);
				} catch (Exception e) {
					exceptionList.add((FormulaGenerateException)e);
				}
			}
		}
		this.newGroovyClass(formulaMap, template,"calculate");
		return exceptionList;
	}
}
